package com.cgw.game.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.renderer.YAxisRendererHorizontalBarChart;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.List;

public class MyYAxisRenderer extends YAxisRendererHorizontalBarChart {
    /**
     * 默认不绘制X Y轴刻度
     */
    protected boolean mIsDrawScale = false;

    public MyYAxisRenderer(ViewPortHandler viewPortHandler,YAxis yAxis,Transformer trans) {
        super(viewPortHandler, yAxis, trans);
    }

    public void setDrawScale(boolean mIsDrawScale) {
        this.mIsDrawScale = mIsDrawScale;
    }
    public boolean isDrawScale() {
        return mIsDrawScale;
    }
    protected float[] mRenderGridLinesBuffer = new float[2];
    /**
     * 绘X轴制刻度线
     * @param c
     */
    public void renderScaleLines(Canvas c) {
        if (!isDrawScale() || !mYAxis.isEnabled())
            return;
        if (mRenderGridLinesBuffer.length != mAxis.mEntryCount * 2) {
            mRenderGridLinesBuffer = new float[mYAxis.mEntryCount * 2];
        }
        float[] positions = mRenderGridLinesBuffer;
        for (int i = 0; i < positions.length; i += 2) {
            positions[i] = mYAxis.mEntries[i / 2];
            positions[i + 1] = mYAxis.mEntries[i / 2];
        }
        mTrans.pointValuesToPixel(positions); //获得X轴点对应的像素点位置 即坐标系位置
        for (int i = 0; i < positions.length - 2; i += 2) {
            //计算y轴两个值之间的间距/4   =  画布偏移量 即 刻度间距 还是默认4个刻度一组
            float offset = (positions[2] - positions[0]) / 4;
            drawScale(c, positions[i], offset);
        }
    }

    /**
     * 绘制线
     */
    protected void drawScale(Canvas canvas, float startX, float offset) {
        float bottomY = mViewPortHandler.contentBottom(); //底部X轴所在的位置
        canvas.save();
         for (int i = 0; i <= 4; i++) {
            canvas.save();
            canvas.translate(offset * i, 0);
           /*if (i % 5 == 0) {
                canvas.drawLine(startX, bottomY - 20, startX, bottomY, mAxisLinePaint);//画长刻度线
            } else {
                canvas.drawLine(startX, bottomY - 10, startX, bottomY, mAxisLinePaint);//画短刻度线
            }*/
            canvas.drawLine(startX, bottomY - 10, startX, bottomY, mAxisLinePaint);//画短刻度线
            canvas.restore();
        }

        canvas.restore();
    }

    @Override
    public void renderLimitLines(Canvas c) {
        super.renderLimitLines(c);
        List<LimitLine> limitLines = mYAxis.getLimitLines();

        if (limitLines == null || limitLines.size() <= 0)
            return;

        float[] pts = mRenderLimitLinesBuffer;
        pts[0] = 0;
        pts[1] = 0;
        pts[2] = 0;
        pts[3] = 0;
        Path limitLinePath = mRenderLimitLinesPathBuffer;
        limitLinePath.reset();

        for (int i = 0; i < limitLines.size(); i++) {

            LimitLine l = limitLines.get(i);

            if (!l.isEnabled())
                continue;

            int clipRestoreCount = c.save();
            mLimitLineClippingRect.set(mViewPortHandler.getContentRect());
            mLimitLineClippingRect.inset(-l.getLineWidth(), 0.f);
            c.clipRect(mLimitLineClippingRect);

            pts[0] = l.getLimit();
            pts[2] = l.getLimit();

            mTrans.pointValuesToPixel(pts);

            pts[1] = mViewPortHandler.contentTop();
            pts[3] = mViewPortHandler.contentBottom();

            limitLinePath.moveTo(pts[0], pts[1]);
            limitLinePath.lineTo(pts[2], pts[3]);

            mLimitLinePaint.setStyle(Paint.Style.STROKE);
            mLimitLinePaint.setColor(Color.WHITE);
            mLimitLinePaint.setStrokeWidth(2f);

            c.drawPath(limitLinePath, mLimitLinePaint);
            limitLinePath.reset();
            c.restoreToCount(clipRestoreCount);
        }

    }
}
